import 'package:flutter/material.dart';
import 'page_state.dart';

class UniversalStateWrapper extends StatelessWidget {
  final PageState state;
  final Widget Function(BuildContext) successBuilder;
  final VoidCallback? onRetry;
  final String? errorMessage;
  final Widget? empty;

  const UniversalStateWrapper({
    super.key,
    required this.state,
    required this.successBuilder,
    this.onRetry,
    this.errorMessage,
    this.empty,
  });

  @override
  Widget build(BuildContext context) {
    switch (state) {
      case PageState.loading:
        return const Center(child: CircularProgressIndicator());
      case PageState.empty:
        return empty ?? const Center(child: Text('No data'));
      case PageState.error:
      case PageState.networkError:
      case PageState.permissionError:
        return Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(errorMessage ?? 'Oops, something went wrong'),
              const SizedBox(height: 12),
              if (onRetry != null)
                ElevatedButton(onPressed: onRetry, child: const Text('Retry')),
            ],
          ),
        );
      case PageState.idle:
      case PageState.success:
        return successBuilder(context);
    }
  }
}